package com.jz.community.moduleshoppingguide.neighbor.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.glideConfig.GlideApp;
import com.jz.community.basecomm.utils.RxDataTool;
import com.jz.community.commview.callback.ErrorCallback;
import com.jz.community.commview.callback.LoadingCallback;
import com.jz.community.moduleshoppingguide.R;
import com.jz.community.moduleshoppingguide.R2;
import com.jz.community.moduleshoppingguide.neighbor.bean.CircleUserInfoBean;
import com.jz.community.moduleshoppingguide.neighbor.presenter.NeighborPersonInfoPresenter;
import com.jz.community.moduleshoppingguide.neighbor.ui.NeighborPersonInfoView;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;

import butterknife.BindView;
import butterknife.OnClick;

public class NeighborPersonInfoActivity extends BaseMvpActivity<NeighborPersonInfoView.View, NeighborPersonInfoPresenter> implements NeighborPersonInfoView.View {


    @BindView(R2.id.circle_user_detail_back)
    ImageView circleUserDetailBack;
    @BindView(R2.id.circle_user_detail_title)
    TextView circleUserDetailTitle;
    @BindView(R2.id.circle_user_detail_toolbar)
    Toolbar circleUserDetailToolbar;
    @BindView(R2.id.circle_user_detail_avatar)
    ImageView circleUserDetailAvatar;
    @BindView(R2.id.circle_user_detail_name)
    TextView circleUserDetailName;
    @BindView(R2.id.circle_user_detail_address)
    TextView circleUserDetailAddress;
    @BindView(R2.id.circle_user_detail_dynamic_iv3)
    ImageView circleUserDetailDynamicIv3;
    @BindView(R2.id.circle_user_detail_dynamic_iv2)
    ImageView circleUserDetailDynamicIv2;
    @BindView(R2.id.circle_user_detail_dynamic_iv1)
    ImageView circleUserDetailDynamicIv1;
    @BindView(R2.id.circle_user_detail_arrow)
    ImageView circleUserDetailArrow;
    @BindView(R2.id.circle_user_detail_dynamic)
    RelativeLayout circleUserDetailDynamic;
    @BindView(R2.id.circle_user_detail_parent)
    LinearLayout circleUserDetailParent;

    private LoadService loadService;

    private String user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentView() {
        return R.layout.module_shoppingguide_activity_neighbor_person_info;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected NeighborPersonInfoPresenter createPresenter() {
        return new NeighborPersonInfoPresenter(this);
    }

    @Override
    protected void initView() {
        super.initView();
        ImmersionBar
                .with(this)
                .titleBar(circleUserDetailToolbar)
                .statusBarDarkFont(true, 0.2f)//当白色背景状态栏遇到不能改变状态栏字体为深色的设备时，解决方案
                .init();

        circleUserDetailTitle.setText("个人资料");

        user_id = getIntent().getStringExtra("user_id");

        loadService = LoadSir.getDefault().register(circleUserDetailParent, (Callback.OnReloadListener) v -> {
            if (loadService.getCurrentCallback() == ErrorCallback.class) {
                loadService.showCallback(LoadingCallback.class);
                mPresenter.initUserInfo(user_id);
            }

        });

        loadService.setCallBack(ErrorCallback.class, (context, view) -> {
            ImageView mIvError = (ImageView) view.findViewById(R.id.error_img);
            mIvError.setImageResource(R.mipmap.error_default);

        });

        loadService.setCallBack(LoadingCallback.class, (context, view) -> {
            ImageView mIvLoading = (ImageView) view.findViewById(R.id.loading_img);
            GlideApp.with(mActivity)
                    .asGif()
                    .load(R.drawable.loading_default)
                    .into(mIvLoading);

        });

    }

    @Override
    protected void initDatas() {
        super.initDatas();
        mPresenter.initUserInfo(user_id);

    }

    @Override
    public void setInfoData(CircleUserInfoBean entity) {
        loadService.showSuccess();
        //头像
        GlideApp.with(mActivity)
                .load(entity.getHeadImage())
                .error(R.mipmap.image_default)
                .into(circleUserDetailAvatar);

        //昵称
        circleUserDetailName.setText(entity.getNickName());

        //圈子名称
        circleUserDetailAddress.setText(entity.getCircleName());

        if (null != entity.getBlogImages() && entity.getBlogImages().size() > 0) {
            if (entity.getBlogImages().size() == 1) {
                if (!RxDataTool.isNullString(entity.getBlogImages().get(0))) {
                    circleUserDetailDynamicIv1.setVisibility(View.VISIBLE);
                    GlideApp.with(mActivity)
                            .load(entity.getBlogImages().get(0))
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(circleUserDetailDynamicIv1);
                } else {
                    circleUserDetailDynamicIv1.setVisibility(View.INVISIBLE);
                }
            } else if (entity.getBlogImages().size() == 2) {
                if (!RxDataTool.isNullString(entity.getBlogImages().get(0))) {
                    circleUserDetailDynamicIv1.setVisibility(View.VISIBLE);
                    GlideApp.with(mActivity)
                            .load(entity.getBlogImages().get(0))
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(circleUserDetailDynamicIv1);
                } else {
                    circleUserDetailDynamicIv1.setVisibility(View.INVISIBLE);
                }

                if (!RxDataTool.isNullString(entity.getBlogImages().get(1))) {
                    circleUserDetailDynamicIv2.setVisibility(View.VISIBLE);
                    GlideApp.with(mActivity)
                            .load(entity.getBlogImages().get(1))
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(circleUserDetailDynamicIv2);
                } else {
                    circleUserDetailDynamicIv2.setVisibility(View.INVISIBLE);
                }
            } else if (entity.getBlogImages().size() >= 3) {
                if (!RxDataTool.isNullString(entity.getBlogImages().get(0))) {
                    circleUserDetailDynamicIv1.setVisibility(View.VISIBLE);
                    GlideApp.with(mActivity)
                            .load(entity.getBlogImages().get(0))
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(circleUserDetailDynamicIv1);
                } else {
                    circleUserDetailDynamicIv1.setVisibility(View.INVISIBLE);
                }

                if (!RxDataTool.isNullString(entity.getBlogImages().get(1))) {
                    circleUserDetailDynamicIv2.setVisibility(View.VISIBLE);
                    GlideApp.with(mActivity)
                            .load(entity.getBlogImages().get(1))
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(circleUserDetailDynamicIv2);
                } else {
                    circleUserDetailDynamicIv2.setVisibility(View.INVISIBLE);
                }

                if (!RxDataTool.isNullString(entity.getBlogImages().get(2))) {
                    circleUserDetailDynamicIv3.setVisibility(View.VISIBLE);
                    GlideApp.with(mActivity)
                            .load(entity.getBlogImages().get(2))
                            .placeholder(R.mipmap.image_default)
                            .error(R.mipmap.image_default)
                            .into(circleUserDetailDynamicIv3);
                } else {
                    circleUserDetailDynamicIv3.setVisibility(View.INVISIBLE);
                }
            }
        }

    }

    @Override
    public void showError(String s) {
        loadService.showCallback(ErrorCallback.class);
        circleUserDetailDynamicIv1.setVisibility(View.INVISIBLE);
        circleUserDetailDynamicIv2.setVisibility(View.INVISIBLE);
        circleUserDetailDynamicIv3.setVisibility(View.INVISIBLE);

    }

    @OnClick(R2.id.circle_user_detail_back)
    public void backClick(View view) {
        finish();
    }

    @OnClick(R2.id.circle_user_detail_dynamic)
    public void dynamicClick(View view) {

    }
}
